'''
Created on Aug 13, 2012

@author: petrina
'''

import logging


class Indicatorset(object):
    '''
    Abstract class which represents a set of indicators.
    
    Included Indicators may be applied to given data from FLEET. The data is handled by the subclasses.
    This set does not perform the indicator calculation. It is just a container for the indicators.
    However, subclasses usually contain a reference to the data which all indicators need for the calculation.
    The given indiconf-parameter defines what indicators are added to the set. If the indiconf is not set,
    all possible indicators will be added to the set, when it is build.
    '''


    def __init__(self, indiconf = None):
        '''
        @param indiconf: the Indicator-configuration is a list containing the indicators-abbreviations or None (= all indicators, default) 
        '''

        # internal set containing the indicators
        self._indiset = set()
        self.indiconf = indiconf
        
        self._name=''

        logging.debug("Indicatorset: created Indicatorset")
        

    def get_indiconf(self):
        return self.__indiconf


    def set_indiconf(self, value):
        self.__indiconf = value


    def del_indiconf(self):
        del self.__indiconf

        
    #----------------------private methods of the class------------------------
    
    def _parse_conf(self, indiconf):
        '''
        Parses the user's configuration on indicator
        
        After parsing the configuration this method
        add only those indicators which the user has set in the configuration.
        
        @param indiconf: indicator configuration (as string)
        '''
        
        # remove all not relevant indicators
        self.build_all_indis()
        toremove = []

        for idx in self._indiset:
            if idx.mr_name.lower().strip() not in indiconf:
                toremove.append(idx)
        for idx in toremove:
            self._indiset.remove(idx)
        

        logging.info("Indicatorset: added all in the config found indicators " + indiconf.__str__())
    
          
    #----------------------public methods of the class-------------------------
        
    def __iter__(self):
        return self._indiset.__iter__()
    
    def next(self):
        pass
    
    def __getitem__(self, key):
        return self.__indiset[key]
    
    def build_all_indis(self):
        '''
        Add all possible indicators to the set (abstract).   
        '''
        pass   
        
    
    def build_indis(self):
        '''
        Adds values to the set depending on the given configuration.
        
        If no user-configuration is available, all indicators are calculated.
        '''
 
        logging.info("Indicatorset: starting to build the Indicatorlist")
        
        # make a new empty set
        self._indiset = set()
        if self.__indiconf is None:
            self.build_all_indis()
        else:
            self._parse_conf(self.__indiconf)    
    
    def create_all_indicators_csv(self, csvwriter):
        
        intervals = [indi.intervals for indi in self._indiset][0] if len(self._indiset) else 0   
        

        header_row = ["intervals"] + [indi._mr_name for indi in self._indiset]
        csvwriter.open_writer('all_indicators_'+self._name+'.csv')
        csvwriter.write(header_row)
        
        for i in range(intervals):
            row = [i] + [indi.values[i] if len(indi.values)==intervals else indi.values[0] for indi in self._indiset]
            csvwriter.write(row)
        csvwriter.close_writer()
    #--------------assign properties using get and set-methods-----------------

    
    indiconf = property(get_indiconf, set_indiconf, del_indiconf, "Indicator configuration is a list containing abbreviations of the indicators or None (means all indicators are in the set)")
